/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChildManager.java
 *
 * Created on Aug 11, 2010, 4:51:10 AM
 */

package GUI;
import config.Database_function;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author QUOCTHGC60050
 */
public class pnf_Manager_Child extends javax.swing.JPanel {
    Connection cn= Database_function.setConnection();
    Statement st;
    ResultSet re;
    Vector table = new Vector();
    Vector label= new Vector();

    public pnf_Manager_Child() {


        initComponents();
        this.setSize(830, 440);
        this.jPanelAdvancedSearch.setVisible(false);

        initTableC();

    }

    public void addTableC()// insert data to table
    {
        try
        {

            Statement st1 = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet ag =st1.executeQuery("select AgeGroup from tbl_AgeGroup where AgeGroupID like '"+re.getString("AgeGroupID")+"'");
            ag.first();
            
            Vector temp= new Vector();

            temp.add(re.getString("ChildID"));
            temp.add(re.getString("FirstName"));
            temp.add(re.getString("LastName"));
            temp.add(ag.getString("AgeGroup")); // get agegroup name from table
            temp.add(re.getString("Gender"));
            temp.add(re.getString("Address"));
            temp.add(re.getString("Birthday"));
            if (re.getString("Status").equals("1"))
                temp.add("True");
            else temp.add("False");
            table.add(temp);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    private void initTableC() // initialize table
    {

        table = new Vector();
        label= new Vector();
        try
        {
            st= cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            re=st.executeQuery("select * from tbl_Child");

            label.add("ID");label.add("FirstName");label.add("LastName");label.add("AgeGroup");label.add("Gender");label.add("Address");label.add("Birthday");label.add("Status"); 

           while(re.next())
            {
                addTableC(); // add data to table
            }

            this.jTableChild.setModel(new DefaultTableModel (table,label));

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void SearchC() // invoke search function
    {

        String searchString= this.jTextFieldSearch.getText();
        String searchType= (String) this.jComboBoxSearch.getSelectedItem();

        try
        {
            st= cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

            table.removeAllElements();

            if (searchType.equals("Name")) // name-based search function
            {
                re=st.executeQuery("select * from tbl_Child where (LastName like '"+searchString+"%') or (FirstName like '"+searchString+"%') order by DIFFERENCE (Firstname, '"+searchString+"') desc");
                while(re.next())
                {
                    addTableC();
                }
            }
            else // other search function
            {
                re=st.executeQuery("select * from tbl_Child where "+searchType+" like '%"+searchString+"%'");
                while(re.next())
                {
                    addTableC();
                }
            }

            this.jTableChild.setModel(new DefaultTableModel (table,label));

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void advancedSearchC() // invoke advanced search function
    {
        String searchString = jTextFieldSearch.getText();
        String searchType = (String) jComboBoxSearch.getSelectedItem();
        String Gender= (String) jComboBoxGender.getSelectedItem();
        String Status= (String) jComboBoxStatus.getSelectedItem();

        String Query="select * from tbl_Child"; // initialize search query
        table.removeAllElements();
        try
        {
            // define name condition
            if (searchType.equals("Name"))
                Query+=" where ((LastName like '"+searchString+"%') or (FirstName like '"+searchString+"%'))";
            else
                Query+=" where ("+searchType+" like '"+searchString+"')";

            // define gender condition
            if (!Gender.equals("Both"))
                Query+="and (Gender like '"+Gender+"')";

            // define status condition
            if (!Status.equals("Both"))
                if (Status.equals("True"))
                    Query+="and (Status like 1)";
                else Query+="and (Status like 0)";

            Date DateS = txtStartDate.getDate();
            Date DateE = txtEndDate.getDate();

            // check null date field
            if (DateS==null)
                DateS = new Date(0000,00,01);
            if (DateE==null)
                DateE = new Date(8099,00,11);

            // build search date string
            String StringS= String.valueOf(DateS.getYear()+1900) + "-" + String.valueOf(DateS.getMonth()+1) + "-" + String.valueOf(DateS.getDate()) ;
            String StringE= String.valueOf(DateE.getYear()+1900) + "-" + String.valueOf(DateE.getMonth()+1) + "-" + String.valueOf(DateE.getDate()) ;

            // validate and define date condition
            if (DateS.before(DateE)==false)
                JOptionPane.showMessageDialog(this, "Start date cannot be more than end date! ","ERROR",JOptionPane.ERROR_MESSAGE,new javax.swing.ImageIcon(getClass().getResource("/image/exit.png")));
            else
                Query+="and ((BirthDay >= '"+StringS+"') and (BirthDay <= '"+StringE+"'))";
    
            Query+="order by DIFFERENCE (Firstname, '"+searchString+"') desc";

            re=st.executeQuery(Query);
            while (re.next())
            {
                addTableC();
            }

            this.jTableChild.setModel(new DefaultTableModel (table,label));
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableChild = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex)
            {
                return false;
            }
        }
        ;
        jButtonSearch = new javax.swing.JButton();
        jCheckBoxAdvancedSearch = new javax.swing.JCheckBox();
        jPanelAdvancedSearch = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtStartDate = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        txtEndDate = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxGender = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxStatus = new javax.swing.JComboBox();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jComboBoxSearch = new javax.swing.JComboBox();
        jTextFieldSearch = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(227, 239, 255));
        setPreferredSize(new java.awt.Dimension(800, 400));

        jTableChild.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "First Name", "Last Name", "AgeGroup", "Gender", "Address", "Status"
            }
        ));
        jTableChild.setGridColor(new java.awt.Color(213, 213, 213));
        jTableChild.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTableChildMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableChild);

        jButtonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Search1.png"))); // NOI18N
        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jCheckBoxAdvancedSearch.setBackground(new java.awt.Color(227, 239, 255));
        jCheckBoxAdvancedSearch.setText("     Advanced Search");
        jCheckBoxAdvancedSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxAdvancedSearchActionPerformed(evt);
            }
        });

        jPanelAdvancedSearch.setBorder(javax.swing.BorderFactory.createTitledBorder("AdvanceSearch"));
        jPanelAdvancedSearch.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanelAdvancedSearch.setEnabled(false);
        jPanelAdvancedSearch.setFocusable(false);
        jPanelAdvancedSearch.setOpaque(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Start Date  :");

        txtStartDate.setDateFormatString("yyyy/MM/dd");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("End Date    :");

        txtEndDate.setDateFormatString("yyyy/MM/dd");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("  Gender        :");

        jComboBoxGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Both", "Male", "Female" }));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Status        :");

        jComboBoxStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Both", "True", "False" }));

        javax.swing.GroupLayout jPanelAdvancedSearchLayout = new javax.swing.GroupLayout(jPanelAdvancedSearch);
        jPanelAdvancedSearch.setLayout(jPanelAdvancedSearchLayout);
        jPanelAdvancedSearchLayout.setHorizontalGroup(
            jPanelAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAdvancedSearchLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(14, 14, 14)
                .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jLabel3)
                .addGap(9, 9, 9)
                .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanelAdvancedSearchLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabel4)
                .addGap(16, 16, 16)
                .addComponent(jComboBoxGender, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(100, 100, 100)
                .addComponent(jLabel5)
                .addGap(16, 16, 16)
                .addComponent(jComboBoxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanelAdvancedSearchLayout.setVerticalGroup(
            jPanelAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAdvancedSearchLayout.createSequentialGroup()
                .addGroup(jPanelAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(10, 10, 10)
                .addGroup(jPanelAdvancedSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/edit_1.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jComboBoxSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Name", "Address", "ChildID" }));

        jLabel1.setText("Search: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63)
                .addComponent(jCheckBoxAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(191, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(212, 212, 212)
                .addComponent(jButtonSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addGap(80, 80, 80)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(78, 78, 78)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jCheckBoxAdvancedSearch)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addComponent(jLabel1))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd)
                    .addComponent(jButtonSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTableChildMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableChildMouseReleased
        // TODO add your handling code here:
        // modify select method and invoke view detail function
        int selectedRow =  jTableChild.getSelectedRow();
        if (evt.getClickCount()==2) {
            Integer ChildID = Integer.parseInt((String)jTableChild.getValueAt(selectedRow, 0));
            jdl_childinfo dc= new jdl_childinfo(null,true,ChildID);
            dc.setVisible(true);
        }
}//GEN-LAST:event_jTableChildMouseReleased

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // TODO add your handling code here:
        if (this.jCheckBoxAdvancedSearch.isSelected())
            advancedSearchC();
        else
            SearchC();
}//GEN-LAST:event_jButtonSearchActionPerformed

    private void jCheckBoxAdvancedSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxAdvancedSearchActionPerformed
        // TODO add your handling code here:
        // select and modify search function
        boolean check = this.jCheckBoxAdvancedSearch.isSelected();
        if (check==true) {
            this.jPanelAdvancedSearch.setVisible(true);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
            this.jComboBoxSearch.addItem("Address");


        } else {
            this.jPanelAdvancedSearch.setVisible(false);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
            this.jComboBoxSearch.addItem("Address");
            this.jComboBoxSearch.addItem("ChildID");
        }
}//GEN-LAST:event_jCheckBoxAdvancedSearchActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        // invoke add child function and refresh table
        AddNewChild an = new AddNewChild();
        an.setVisible(true);
        initTableC();
}//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        // invoke edit child function and refresh table
        int d=this.jTableChild.getSelectedRow();
        if(d>=0) {
            String idChild=(String)this.jTableChild.getValueAt(d, 0);
            EditChild ed = new EditChild(idChild);
            ed.setVisible(true);
          initTableC();
        }
        else {
            JOptionPane.showMessageDialog(this, "You must choose a child to edit! ","ERROR",JOptionPane.ERROR_MESSAGE,new javax.swing.ImageIcon(getClass().getResource("/image/Exit.png")));
        }
         
       
}//GEN-LAST:event_btnEditActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JCheckBox jCheckBoxAdvancedSearch;
    private javax.swing.JComboBox jComboBoxGender;
    private javax.swing.JComboBox jComboBoxSearch;
    private javax.swing.JComboBox jComboBoxStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanelAdvancedSearch;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableChild;
    private javax.swing.JTextField jTextFieldSearch;
    private com.toedter.calendar.JDateChooser txtEndDate;
    private com.toedter.calendar.JDateChooser txtStartDate;
    // End of variables declaration//GEN-END:variables

}
